﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using AnyOffice.Model;
using log4net;
using System.Reflection;

namespace AnyOffice.general.rms.roll_file
{
    public partial class edit : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

       
        private void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件删除", exception);
                    }
                }
            }
        }
        protected override void OnInit(EventArgs e)
        {
            this.btnRemoveFile.Click += new EventHandler(this.btnRemoveFile_Click);
            this.btnAddFile.Click += new EventHandler(this.btnAddFile_Click);
            this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
            base.OnInit(e);
        }      
 

        private void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件添加", exception);
                    }
                }
            }
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string attachmentId = "";
            try
            {
                if (this.ViewState["OldAttachmentId"].ToString() != string.Empty)
                {
                    AttachmentBiz.DeleteByAttachmendId(base.Conn, this.ViewState["OldAttachmentId"].ToString());
                }
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 0;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("表单提交 - 附件上传相关", exception);
                }
            }
            RmsFileData data2 = new RmsFileData();
            data2.FileId = Convert.ToInt32(this.ViewState["FileId"].ToString());
            data2.FileCode = this.txtFileCode.Text;
            if (this.txtFileSubject.Text != string.Empty)
            {
                data2.FileSubject = this.txtFileSubject.Text;
            }
            data2.FileTitle = this.txtFileTitle.Text;
            if (this.txtFileTitle0.Text != string.Empty)
            {
                data2.FileTitle0 = this.txtFileTitle0.Text;
            }
            if (this.txtSendUnit.Text != string.Empty)
            {
                data2.SendUnit = this.txtSendUnit.Text;
            }
            if (this.txtSendDate.Value != string.Empty)
            {
                data2.SendDate = Convert.ToDateTime(this.txtSendDate.Value);
            }
            if (this.ddlSecret.SelectedValue != string.Empty)
            {
                data2.Secret = this.ddlSecret.SelectedValue;
            }
            if (this.ddlUrgency.SelectedValue != string.Empty)
            {
                data2.Urgency = this.ddlUrgency.SelectedValue;
            }
            if (this.ddlFileKind.SelectedValue != string.Empty)
            {
                data2.FileKind = this.ddlFileKind.SelectedValue;
            }
            if (this.ddlFileType.SelectedValue != string.Empty)
            {
                data2.FileType = this.ddlFileType.SelectedValue;
            }
            if (this.txtFilePage.Text != string.Empty)
            {
                data2.FilePage = this.txtFilePage.Text;
            }
            if (this.txtPrintPage.Text != string.Empty)
            {
                data2.PrintPage = this.txtPrintPage.Text;
            }
            if (this.txtRemark.Text != string.Empty)
            {
                data2.Remark = this.txtRemark.Text;
            }
            if (this.ddlRollId.SelectedValue != string.Empty)
            {
                data2.RollId = Convert.ToInt32(this.ddlRollId.SelectedValue);
            }
            if (attachmentId != string.Empty)
            {
                data2.AttachmentId = attachmentId;
            }
            data2.ModUser = base.User.Identity.Name;
            data2.ModTime = DateTime.Now;
            new RmsFileDao().Update(base.Conn, data2);
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("文件修改成功！", "index1.aspx");
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["page"] != null) ? int.Parse(Request.QueryString["page"].ToString()) : 1;
                this.ViewState["CurrentPage"] = num;
                int fileId = (Request.QueryString["file_id"] != null) ? int.Parse(Request.QueryString["file_id"].ToString()) : 0;
                this.ViewState["FileId"] = fileId;
                this.ddlSecret.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Secret);
                this.ddlSecret.DataBind();
                this.ddlSecret.Items.Insert(0, new ListItem("", ""));
                this.ddlUrgency.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.Urgency);
                this.ddlUrgency.DataBind();
                this.ddlUrgency.Items.Insert(0, new ListItem("", ""));
                this.ddlFileKind.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.FileKind);
                this.ddlFileKind.DataBind();
                this.ddlFileKind.Items.Insert(0, new ListItem("", ""));
                this.ddlFileType.DataSource = new RmsCodeDao().SelectAll(base.Conn, RmsCodeDao.CodeType.FileType);
                this.ddlFileType.DataBind();
                this.ddlFileType.Items.Insert(0, new ListItem("", ""));
                this.ddlRollId.DataSource = new RmsRollDao().SelectOpen(base.Conn);
                this.ddlRollId.DataBind();
                this.ddlRollId.Items.Insert(0, new ListItem("", ""));
                DataTable table = new RmsFileDao().SelectByFileId(base.Conn, fileId);
                if (table.Rows.Count > 0)
                {
                    this.txtFileCode.Text = table.Rows[0]["FileCode"].ToString();
                    if (table.Rows[0]["FileSubject"] != DBNull.Value)
                    {
                        this.txtFileSubject.Text = table.Rows[0]["FileSubject"].ToString();
                    }
                    if (table.Rows[0]["FileTitle"] != DBNull.Value)
                    {
                        this.txtFileTitle.Text = table.Rows[0]["FileTitle"].ToString();
                    }
                    if (table.Rows[0]["FileTitle0"] != DBNull.Value)
                    {
                        this.txtFileTitle0.Text = table.Rows[0]["FileTitle0"].ToString();
                    }
                    if (table.Rows[0]["SendUnit"] != DBNull.Value)
                    {
                        this.txtSendUnit.Text = table.Rows[0]["SendUnit"].ToString();
                    }
                    if (table.Rows[0]["SendDate"] != DBNull.Value)
                    {
                        this.txtSendDate.Value = Convert.ToDateTime(table.Rows[0]["SendDate"].ToString()).ToString("yyyy-MM-dd");
                    }
                    if ((table.Rows[0]["Secret"] != DBNull.Value) && (this.ddlSecret.Items.FindByValue(table.Rows[0]["Secret"].ToString()) != null))
                    {
                        this.ddlSecret.Items.FindByValue(table.Rows[0]["Secret"].ToString()).Selected = true;
                    }
                    if ((table.Rows[0]["Urgency"] != DBNull.Value) && (this.ddlUrgency.Items.FindByValue(table.Rows[0]["Urgency"].ToString()) != null))
                    {
                        this.ddlUrgency.Items.FindByValue(table.Rows[0]["Urgency"].ToString()).Selected = true;
                    }
                    if ((table.Rows[0]["FileKind"] != DBNull.Value) && (this.ddlFileKind.Items.FindByValue(table.Rows[0]["FileKind"].ToString()) != null))
                    {
                        this.ddlFileKind.Items.FindByValue(table.Rows[0]["FileKind"].ToString()).Selected = true;
                    }
                    if ((table.Rows[0]["FileType"] != DBNull.Value) && (this.ddlFileType.Items.FindByValue(table.Rows[0]["FileType"].ToString()) != null))
                    {
                        this.ddlFileType.Items.FindByValue(table.Rows[0]["FileType"].ToString()).Selected = true;
                    }
                    if (table.Rows[0]["FilePage"] != DBNull.Value)
                    {
                        this.txtFilePage.Text = table.Rows[0]["FilePage"].ToString();
                    }
                    if (table.Rows[0]["PrintPage"] != DBNull.Value)
                    {
                        this.txtPrintPage.Text = table.Rows[0]["PrintPage"].ToString();
                    }
                    if (table.Rows[0]["Remark"] != DBNull.Value)
                    {
                        this.txtRemark.Text = table.Rows[0]["Remark"].ToString();
                    }
                    if (this.ddlRollId.Items.FindByValue(table.Rows[0]["RollId"].ToString()) != null)
                    {
                        this.ddlRollId.Items.FindByValue(table.Rows[0]["RollId"].ToString()).Selected = true;
                    }
                }
                else
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此案卷", "index1.aspx");
                }
                string attachmentId = (table.Rows[0]["AttachmentId"] != DBNull.Value) ? table.Rows[0]["AttachmentId"].ToString() : "";
                this.ViewState["FileNums"] = 0;
                this.ViewState["FileList"] = new ArrayList();
                this.ViewState["UniqueTempPath"] = "";
                this.ViewState["TotalSize"] = 0;
                this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                this.ViewState["OldAttachmentId"] = attachmentId;
                if (attachmentId != string.Empty)
                {
                    DataTable table2 = new AttachmentDao().SelectByAttachmentId(base.Conn, attachmentId);
                    if (table2.Rows.Count > 0)
                    {
                        try
                        {
                            this.ViewState["FileNums"] = table2.Rows.Count;
                            ArrayList list = new ArrayList();
                            string savedPath = AttachmentBiz.GetSavedPath(attachmentId);
                            string uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                            this.ViewState["UniqueTempPath"] = uniqueTempPath;
                            AttachmentBiz.Copy(savedPath, uniqueTempPath);
                            int num3 = 0;
                            for (int i = 0; i < table2.Rows.Count; i++)
                            {
                                list.Add(table2.Rows[i]["Name"].ToString());
                                num3 += Convert.ToInt32(table2.Rows[i]["TotalSize"].ToString());
                            }
                            this.ltbFileList.DataSource = list;
                            this.ltbFileList.DataBind();
                            this.ltbFileList.Rows = table2.Rows.Count;
                            this.ViewState["FileList"] = list;
                            this.ViewState["TotalSize"] = num3;
                            this.pnlAttach.Visible = true;
                        }
                        catch (Exception exception)
                        {
                            if (log.IsErrorEnabled)
                            {
                                log.Error("提交前 - 附件上传相关", exception);
                            }
                        }
                    }
                }
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
            }


        }
    }
}
